<?php
namespace app\commands;

use app\models\Activity;
use app\models\GoodsVisitLog;
use app\models\Order;
use yii\console\Controller;

class ScheduleController extends Controller
{
    /**
     * 对系统中订单定时处理
     * ./yii schedule/order-schedule
     */
    public function actionOrderSchedule()
    {
        $this->approveOrderFlow();
        $this->payOrderFlow();
        $this->syncOrderFlow();
    }

    /**
     * 将所有无需审核订单的机构中未审核的流转订单进行审核
     */
    private function approveOrderFlow()
    {
        echo "start approveOrderFlow....\n";

        // 获取不需要审核订单的机构中得未审核流转订单
        $cmd = \Yii::$app->db->createCommand('
            SELECT * FROM t_order_flow t0
            LEFT JOIN t_institution t1 ON t1.`code`=t0.institution_code
            WHERE `status`=10 AND t1.is_auto_order=1
            ORDER BY t0.create_datetime DESC');
        $items = $cmd->queryAll();
        foreach ($items as $item) {
            echo $item['id'];
            Order::approveFlow($item['id']);
            echo " ok\n";
        }

        echo "approveOrderFlow is ok.\n";
    }

    /**
     * 将所有机构未扣款的流转订单进行机构扣款处理
     */
    private function payOrderFlow()
    {
        echo "start payOrderFlow....\n";

        $cmd = \Yii::$app->db->createCommand('SELECT * FROM t_order_flow WHERE `status`=12 ORDER BY create_datetime DESC');
        $items = $cmd->queryAll();
        foreach ($items as $item) {
            echo $item['id'];
            Order::payFlow($item['id']);
            echo " ok\n";
        }

        echo "payOrderFlow is ok.\n";
    }

    /**
     * 将所有未能提交到业务系统的流转订单提交到业务系统
     * @throws \Exception
     */
    private function syncOrderFlow()
    {
        echo "start syncOrderFlow....\n";

        $cmd = \Yii::$app->db->createCommand("
            SELECT * FROM t_order_flow t0
            LEFT JOIN t_institution t1 ON t1.`code`=t0.institution_code
            WHERE `status`=13 AND t1.parent_code='0'
            ORDER BY t0.create_datetime DESC");
        $items = $cmd->queryAll();
        foreach ($items as $item) {
            echo $item['order_code'];
            Order::sync($item['order_code']);
            echo " ok\n";
        }

        echo "syncOrderFlow is ok.\n";
    }

    /**
     * 定时对系统中的活动进行处理
     */
    public function actionActivitySchedule()
    {
        Activity::autoUpdateBargain();
    }

    /**
     * 定时将缓存中的商品访问日志同步到数据库中
     * @throws \yii\db\Exception
     */
    public function actionSyncGoodsVisitLog()
    {
        $page = 0;
        do {
            $items = GoodsVisitLog::getList($page++, 1000);

            if (count($items) > 0) {
                $sql = '';
                foreach ($items as $item) {
                    $createDatetime = date('Y-m-d H:i:s', $item['createDatetime']);
                    $sql = $sql . "call p_set_goods_visit_log('{$item['_id']}', '{$item['goodsCode']}', '{$item['userId']}', '{$item['institutionCode']}', '{$createDatetime}');";
                }

                $cmd = \Yii::$app->db->createCommand($sql);
                $cmd->execute();
            }
        } while (count($items) > 0);
    }
}